﻿using Microsoft.Extensions.Hosting;
using PeterKottas.DotNetCore.WindowsService.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace HS.EIP.WatchService
{

    class ServiceHost : PeterKottas.DotNetCore.WindowsService.Base.MicroService, IMicroService
    {
        public ServiceHost()
        {
            NLogHelp.InfoLog("开始初始化 服务守护服务。");
            Debug.WriteLine("开始初始化 服务守护服务。");
        }
        private IHost _host;
        private ILogger logger = null;
        public void Start()
        {
            try
            {
                NLogHelp.InfoLog("开始启动 服务守护服务。");
                this._host = InitHost();
                this.logger = this._host.Services.GetRequiredService<ILoggerFactory>().CreateLogger<ServiceHost>();
                this._host.StartAsync();
                NLogHelp.InfoLog("成功启动 服务守护服务。");
            }
            catch (Exception ex)
            {
                NLogHelp.ErrorLog("启动失败 服务守护服务。", ex);
                this.logger?.LogError(ex, "启动失败 服务守护服务。");
            }
        }
        public void Stop()
        {
            this._host?.StopAsync().Wait();
            NLogHelp.InfoLog("成功停止 消息队列处理服务。");
        }
        IHost InitHost()
        {
            return new HostBuilder()
                .ConfigureAppConfiguration((configBuilder) =>
                {
                    configBuilder.SetBasePath(Directory.GetCurrentDirectory());
                    configBuilder.AddEnvironmentVariables();
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        NLogHelp.InfoLog("开始加载【appsettings-linux.json】配置文件");
                        Console.WriteLine("开始加载【appsettings-linux.json】配置文件");
                        configBuilder.AddJsonFile("appsettings-linux.json", true);
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        NLogHelp.InfoLog("开始加载【appsettings-windows.json】配置文件");
                        Console.WriteLine("开始加载【appsettings-windows.json】配置文件");
                        configBuilder.AddJsonFile("appsettings-windows.json", true);
                    }
                    else throw new NotImplementedException("不支持的操作系统");


                })
            .ConfigureLogging((context, loggingBuilder) =>
            {
                loggingBuilder.AddConfiguration(context.Configuration.GetSection("Logging"));//这句一定要加
                loggingBuilder.AddConsole();
                //this.logger = loggingBuilder.Services.CreateLogger<ServiceHost>();
            })
               .ConfigureServices((context, services) =>
               {
                   //logger.LogDebug("服务初始化完成");

                   services.Configure<HostOptions>(option =>
                   {
                       option.ShutdownTimeout = System.TimeSpan.FromSeconds(20);
                   });

                   services.AddHostedService<WatchHostService>();

                   //new EIPBuilder(services, context.Configuration);
               })
           .Build();
        }
    }
}
